#include "DopplerDataView.h"
#include "DopplerRulerBar.h"
#include "DopplerTitleBar.h"
#include "DopplerColorBar.h"
#include "DopplerGraphicView.h"
#include "DopplerGraphicsItem.h"
#include <QGridLayout>
#include <math.h>

static const QSize DEFAULT_SIZE( 800 , 600);
static const QSize MINIMUM_SIZE( 100 , 100);

DopplerDataView::DopplerDataView(QWidget *parent , DATA_VIEW_COMPONENT eComponent_) :
    QWidget(parent)
{
    m_pLayout       = NULL  ;
    m_pTitleBar     = NULL  ;
    m_pColorBar     = NULL  ;
    m_pGraphicView  = NULL  ;
    m_eComponent    = eComponent_  ;

    for(int i = 0; i < DATA_VIEW_RULER_MAX ; i++)
    {
        m_pRulers[i] = NULL ;
        RulerRange[i].first  = 0     ;
        RulerRange[i].second = 100   ;
    }

    setMinimumSize(MINIMUM_SIZE);
    resize(DEFAULT_SIZE);
    //************ set back ground color
    //QPalette pal = this->palette() ;
    //pal.setColor(QPalette::Background , QColor(0 , 0 , 0));
    //setPalette(pal);
    //setAutoFillBackground(true);
    setFont(QFont("simsun" ,9));
    //*********** create widgets of this view
    CreateComponent() ;

}


DopplerDataView::~DopplerDataView()
{
    DeleteAllWidget();
}

void DopplerDataView::DeleteAllWidget()
{
    for(int i = 0; i < DATA_VIEW_RULER_MAX ; i++)
    {
        delete m_pRulers[i] ;
        m_pRulers[i] = NULL ;
    }

    if(m_pTitleBar     != NULL)  {delete m_pTitleBar     ;   m_pTitleBar  =  NULL ;}
    if(m_pColorBar     != NULL)  {delete m_pColorBar     ;   m_pColorBar  =  NULL ;}
    if(m_pGraphicView  != NULL)  {delete m_pGraphicView  ;   m_pGraphicView  =  NULL ;}
    if(m_pLayout       != NULL)  {delete m_pLayout       ;   m_pLayout  =  NULL ;}
}


void DopplerDataView::EnableComponent(DATA_VIEW_COMPONENT eComponent_)
{
    m_eComponent    = eComponent_  ;
    CreateComponent() ;
}

void DopplerDataView::SetRulerRange(double nStart_ , double nStop_ , DATA_VIEW_RULER eRuler_ )
{
    if(eRuler_ >= DATA_VIEW_RULER_MAX)  return ;
    RulerRange[eRuler_].first  = nStart_  ;
    RulerRange[eRuler_].second = nStop_   ;
    if(m_pRulers[eRuler_])  m_pRulers[eRuler_]->SetMarkerRange(nStart_ , nStop_);
    m_pGraphicView->slotResetView();
}

void DopplerDataView::SetRulerColor(QColor* color , DATA_VIEW_RULER eRuler_)
{
    if(m_pRulers[eRuler_])
        m_pRulers[eRuler_]->SetBackgroudColor(color);
}
void DopplerDataView::SetRulerMarkerColor(QColor* color , DATA_VIEW_RULER eRuler_ )
{
    if(m_pRulers[eRuler_])
        m_pRulers[eRuler_]->SetMarkerColor(color);
}

void DopplerDataView::SetRulerUnit(QString* unit  , DATA_VIEW_RULER eRuler_)
{
    if(m_pRulers[eRuler_])
        m_pRulers[eRuler_]->SetMarkerUnit(unit);
}

void DopplerDataView::SetTitleBarColor(QColor* color_)
{
    if(m_pTitleBar)
        m_pTitleBar->SetBackgroudColor(color_);
}

void DopplerDataView::SetTitleBarString(QString* str_)
{
    if(m_pTitleBar)
        m_pTitleBar->SetTitle(str_);
}

void DopplerDataView::SetTitleBarStringColor(QColor* color_)
{
    if(m_pTitleBar)
        m_pTitleBar->SetTextColor(color_);
}

void DopplerDataView::SetColorBarColorIndex(DopplerColorIndex* index)
{
    if(m_pColorBar)
        m_pColorBar->setColorIndex(index);
}

void DopplerDataView::AddOverlayItems(QGraphicsItem* item_)
{
    if(m_pGraphicView)
        m_pGraphicView->AddOverlayItems(item_);
}


QSize DopplerDataView::GetViewSize()
{
    return m_pGraphicView->GetSceneSize();
}

void DopplerDataView::SetDrawScan(DopplerDrawScan* pDraw_)
{
    m_pGraphicView->SetDrawScan(pDraw_);
}

void DopplerDataView::UpdateDrawing()
{
    m_pGraphicView->UpdateDrawing();
}

void DopplerDataView::SetItemGeometry(DopplerGraphicsItem* item , QRectF& rect_)
{
    double _nVStart = RulerRange[DATA_VIEW_RULER_LEFT].first  ;
    double _nVStop  = RulerRange[DATA_VIEW_RULER_LEFT].second ;
    double _nVHeight= _nVStop - _nVStart ;

    double _nHStart = RulerRange[DATA_VIEW_RULER_BOTTOM].first  ;
    double _nHStop  = RulerRange[DATA_VIEW_RULER_BOTTOM].second ;
    double _nHWidth = _nHStop - _nHStart ;

    double _nPosLeft   = rect_.left()   ;
    double _nPosRight  = rect_.right()  ;
    double _nPosTop    = rect_.top()    ;
    double _nPosBottom = rect_.bottom() ;
    int _nSceneWidth = m_pGraphicView->GetSceneSize().width()  ;
    int _nSceneHeight= m_pGraphicView->GetSceneSize().height() ;

    _nPosLeft = _nSceneWidth * (_nPosLeft - _nHStart) / _nHWidth  ;
    _nPosRight = _nSceneWidth * (_nPosRight - _nHStart) / _nHWidth  ;
    _nPosTop   = _nSceneHeight * (_nPosTop - _nVStart) / _nVHeight  ;
    _nPosBottom = _nSceneHeight * (_nPosBottom - _nVStart) / _nVHeight  ;
    QRectF _rect(fabs(_nPosLeft) , fabs(_nPosTop) , fabs(_nPosRight - _nPosLeft) , fabs(_nPosBottom - _nPosTop) ) ;
    //qDebug("geometry %f %f %f %f " , _rect.left() , _rect.top() , _rect.width() , _rect.height());
    item->SetItemGeometry(_rect);
    item->SetItemGeometryReal(rect_);
}

//********************************************************
//     create rulers \ title \ color bar for this window
//********************************************************
void DopplerDataView::CreateComponent()
{
    DeleteAllWidget();
    m_pLayout =new QGridLayout(this) ;
    //***** space between widget
    m_pLayout->setSpacing(0);
    //***** space offset of layout in parent window
    m_pLayout->setContentsMargins(0 , 0 , 0 , 0);

    QSize _size = this->size() ;
    int _nWidth = _size.width() ;
    int _nHeight= _size.height() ;
    //***************************
    //*   create widget of each cell
    //***************************
    if(m_eComponent & DATA_VIEW_COMPONENT_TITLE)
    {
        m_pTitleBar  = new DopplerTitleBar(this) ;
        //m_pTitleBar->hide();
        m_pLayout->addWidget(m_pTitleBar , 0 , 1);
        _nHeight -=  m_pTitleBar->height() ;

    }

    if(m_eComponent & DATA_VIEW_COMPONENT_LEFTRULER)
    {
        m_pRulers[DATA_VIEW_RULER_LEFT] = new DopplerRulerBar(this , DopplerRulerBar::RULER_BAR_LEFT) ;
        //m_pRulers[DATA_VIEW_RULER_LEFT]->hide();
        m_pLayout->addWidget(m_pRulers[DATA_VIEW_RULER_LEFT] , 1 , 0);
        m_pRulers[DATA_VIEW_RULER_LEFT]->SetMarkerRange(RulerRange[DATA_VIEW_RULER_LEFT].first , RulerRange[DATA_VIEW_RULER_LEFT].second);
        _nWidth -= m_pRulers[DATA_VIEW_RULER_LEFT]->width() ;
    }

    if(m_eComponent & DATA_VIEW_COMPONENT_RIGHTRULER)
    {
        m_pRulers[DATA_VIEW_RULER_RIGHT] = new DopplerRulerBar(this , DopplerRulerBar::RULER_BAR_RIGHT) ;
        //m_pRulers[DATA_VIEW_RULER_RIGHT]->hide();
        m_pLayout->addWidget(m_pRulers[DATA_VIEW_RULER_RIGHT] , 1 , 3);
        m_pRulers[DATA_VIEW_RULER_RIGHT]->SetMarkerRange(RulerRange[DATA_VIEW_RULER_RIGHT].first , RulerRange[DATA_VIEW_RULER_RIGHT].second);
        _nWidth -= m_pRulers[DATA_VIEW_RULER_RIGHT]->width() ;
    }

    if(m_eComponent & DATA_VIEW_COMPONENT_BOTTOMRULER)
    {
        m_pRulers[DATA_VIEW_RULER_BOTTOM] = new DopplerRulerBar(this , DopplerRulerBar::RULER_BAR_BOTTOM) ;
        //m_pRulers[DATA_VIEW_RULER_BOTTOM]->hide();
        m_pLayout->addWidget(m_pRulers[DATA_VIEW_RULER_BOTTOM] , 2 , 1);
        m_pRulers[DATA_VIEW_RULER_BOTTOM]->SetMarkerRange(RulerRange[DATA_VIEW_RULER_BOTTOM].first , RulerRange[DATA_VIEW_RULER_BOTTOM].second);
        _nHeight -=  m_pRulers[DATA_VIEW_RULER_BOTTOM]->height() ;
    }

    if(m_eComponent & DATA_VIEW_COMPONENT_COLORBAR)
    {
        m_pColorBar = new DopplerColorBar(this) ;
        //m_pColorBar->hide();
        m_pLayout->addWidget(m_pColorBar , 1, 2);
        _nWidth -= m_pColorBar->width() ;
    }

    //***************************************
    //** center widget for data displaying
    //***************************************
    m_pGraphicView = new DopplerGraphicView(this , QSize(_nWidth , _nHeight)) ;
    //m_pGraphicView->hide();
    m_pLayout->addWidget(m_pGraphicView , 1 , 1)  ;
    connect(m_pGraphicView , SIGNAL(signalViewChanged(QRectF)) , this , SLOT(slotZoonAction(QRectF)));
    connect(m_pGraphicView, SIGNAL(signalItemMoved(DopplerGraphicsItem*)) , this , SLOT(slotItemMoved(DopplerGraphicsItem*)));
    setLayout(m_pLayout);
}


void DopplerDataView::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);

    QSize _size = size() ;
    int _nWidth = _size.width() ;
    int _nHeight= _size.height() ;
    //***************************
    //*   create widget of each cell
    //***************************
    if(m_pTitleBar)
        _nHeight -=  m_pTitleBar->height() ;

    if(m_pRulers[DATA_VIEW_RULER_LEFT])
        _nWidth -= m_pRulers[DATA_VIEW_RULER_LEFT]->width() ;

    if(m_pRulers[DATA_VIEW_RULER_RIGHT])
        _nWidth -= m_pRulers[DATA_VIEW_RULER_RIGHT]->width() ;

    if(m_pRulers[DATA_VIEW_RULER_BOTTOM])
        _nHeight -=  m_pRulers[DATA_VIEW_RULER_BOTTOM]->height() ;

    if(m_pColorBar)
        _nWidth -= m_pColorBar->width() ;

    m_pGraphicView->resize(_nWidth , _nHeight);

}

void DopplerDataView::paintEvent(QPaintEvent* event)
{
    QWidget::paintEvent(event) ;
}

void DopplerDataView::RangeTransfer(QPair<double , double> range_ , QPair<double , double>* pos_)
{
    double _nRange = range_.second  - range_.first  ;
    pos_->first = _nRange * pos_->first + range_.first  ;
    pos_->second= _nRange * pos_->second + range_.first ;
}

void DopplerDataView::slotZoonAction(QRectF rect)
{
    if(m_eComponent & DATA_VIEW_COMPONENT_LEFTRULER)
    {
        QPair<double , double> _marker ;
        _marker.first = rect.top() ;
        _marker.second= rect.bottom() ;
        RangeTransfer(RulerRange[DATA_VIEW_RULER_LEFT] , &_marker) ;
        m_pRulers[DATA_VIEW_RULER_LEFT]->SetMarkerRange(_marker.first , _marker.second);
    }

    if(m_eComponent & DATA_VIEW_COMPONENT_BOTTOMRULER)
    {
        QPair<double , double> _marker ;
        _marker.first = rect.left() ;
        _marker.second= rect.right() ;
        RangeTransfer(RulerRange[DATA_VIEW_RULER_BOTTOM] , &_marker) ;
        m_pRulers[DATA_VIEW_RULER_BOTTOM]->SetMarkerRange(_marker.first , _marker.second);
    }

}

void DopplerDataView::slotItemMoved(DopplerGraphicsItem* item_)
{
    QPointF _pos1 = item_->GetItemScenePos()  ;
    QPointF _pos2 = PixTransferToReal(_pos1)  ;
    QRectF _rect = item_->GetItemGeometryReal();
    _rect = QRectF(_pos2.x() , _pos2.y() , _rect.width() , _rect.height())  ;
    item_->SetItemGeometryReal(_rect);
}

QPointF DopplerDataView::PixTransferToReal(QPointF& pos_)
{
    double _nPosX = pos_.x() ;
    double _nPosY = pos_.y() ;

    double _nVStart = RulerRange[DATA_VIEW_RULER_LEFT].first  ;
    double _nVStop  = RulerRange[DATA_VIEW_RULER_LEFT].second ;
    double _nVHeight= _nVStop - _nVStart ;

    double _nHStart = RulerRange[DATA_VIEW_RULER_BOTTOM].first  ;
    double _nHStop  = RulerRange[DATA_VIEW_RULER_BOTTOM].second ;
    double _nHWidth = _nHStop - _nHStart ;

    int _nSceneWidth = m_pGraphicView->GetSceneSize().width()  ;
    int _nSceneHeight= m_pGraphicView->GetSceneSize().height() ;

    _nPosX  = _nHWidth * _nPosX / _nSceneWidth +  _nHStart  ;
    _nPosY  = _nVHeight* _nPosY / _nSceneHeight + _nVStart  ;


    return QPointF(_nPosX , _nPosY) ;
}
